package org.springblade.modules.api.controller;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 生成二维码
 */
@RestController
@RequestMapping("/qrCode")
@Api(tags = "二维码生成")
@Slf4j
public class QrCode {


	private static final String RootPath="D:\\1.erwerima";
	private static final String FileFormat=".png";

	private static final ThreadLocal<SimpleDateFormat> LOCALDATEFORMAT=ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMMddHHmmss"));

	//生成二维码并将其存放于本地目录
	@PostMapping("generate/v1")
	public R generateV1(String content){
		try {
			final String fileName=LOCALDATEFORMAT.get().format(new Date());
			QRCodeUtil.createCodeToFile(content,new File(RootPath),fileName+FileFormat);
		}catch (Exception e){
			log.info(e.getMessage());
		}
		return R.success("成功");
	}

	//生成二维码并将其返回给前端调用者
	@PostMapping("generate/v2")
	public  void   generateV2(String content, HttpServletResponse servletResponse){
		try {
			QRCodeUtil.createCodeToOutputStream(content,servletResponse.getOutputStream());
		}catch (Exception e){
			log.info(e.getMessage());
		}
//		return  R.success("成功") ;
	}

}
